﻿using Shop.Collect;
using Shop.ExtendService;
using Shop.Model.DB;
using Shop.RemoteModel.SpecTemplate.Model;
using Shop.Service.Interface;

namespace Shop.Service.lmpl
{
    internal class SpecsTemplateService : ISpecsTemplateService
    {
        private readonly ISpecsTemplateCollect _Service;
        private readonly IFileService _File;
        public SpecsTemplateService (ISpecsTemplateCollect service, IFileService file)
        {
            this._File = file;
            this._Service = service;
        }

        public long Add (SpecTemplateAdd datum)
        {
            long id = this._Service.Add(datum);
            if (datum.FileId.HasValue)
            {
                this._File.Save(datum.FileId.Value, id);
            }
            return id;
        }

        public void Delete (long id)
        {
            DBSpecsTemplate source = this._Service.Get(id);
            this._Service.Delete(source);
        }
        public Dictionary<long, int> SetSort (long id, int sort)
        {
            DBSpecsTemplate source = this._Service.Get(id);
            return this._Service.SetSort(source, sort);
        }
        public SpecTemplateDto Get (long id)
        {
            return this._Service.Get<SpecTemplateDto>(id);
        }
        public SpecTemplateDto[] Gets (long templateId)
        {
            return this._Service.Gets(templateId);
        }
        public SpecTemplateItem[] GetItems (long templateId, string query)
        {
            return this._Service.GetItems(templateId, query);
        }
        public bool Set (long id, SpecTemplateSet set)
        {
            DBSpecsTemplate source = this._Service.Get(id);
            string icon = source.SpecIcon;
            if (this._Service.Set(source, set))
            {
                this._File.Sync(set.FileId, id, icon);
                return true;
            }
            return false;
        }

        public void SetIsEnable (long id, bool isEnable)
        {
            DBSpecsTemplate source = this._Service.Get(id);
            this._Service.SetIsEnable(source, isEnable);
        }
    }
}
